package cn.eleme.meal.utils;

import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Administrator
 */
@Component
public class OperationUtils {

    @Value("${eleme.param.queryOrderListUrl}")
    private String queryOrderListUrl ;
    @Value("${eleme.param.clickButUrl}")
    private String clickButUrl ;


    /**
     * TODO 分离出店铺id
     * @return
     */
    public String readListInfo(Map<String,Object> query){
        JSONObject jsonObject = new JSONObject() ;
        jsonObject.put("method","OrderService.queryLatestOrderForPC");
        jsonObject.put("id",query.get("requestId").toString());
        HashMap map = new HashMap<String,String>();
        map.put("appName","melody");
        map.put("appVersion","1.1.1");
        map.put("key","1.0.0");
        map.put("ksid",String.valueOf(query.get("ksid")));
        jsonObject.put("metas",map);

        jsonObject.put("method","queryLatestOrderForPC");
        jsonObject.put("ncp","2.0.0");

        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("orderFilter","ORDER_QUERY_PROCESSING_V3");
        paramMap.put("shopId",query.get("shopId"));
        Map<String,Object> conditionMap = new HashMap<>();
        conditionMap.put("beginTime",null);
        conditionMap.put("bookingOrderType",null);
        conditionMap.put("endTime",null);
        conditionMap.put("limit",20);
        conditionMap.put("offset",0);
        paramMap.put("condition",conditionMap);
        jsonObject.put("params",paramMap);
        jsonObject.put("service","OrderService");


        Map<String, String> header = new HashMap<String, String>();
        header.put(":authority","app-api.shop.ele.me");
        header.put(":method","POST");
        header.put(":path","/nevermore/invoke/?method=OrderService.queryLatestOrderForPC");
        header.put(":scheme","https");
        header.put("accept-encoding","gzip, deflate");
        header.put("accept", "application/json, text/plain, */*");
        header.put("Accept-Language", "zh-CN,zh;q=0.8");
        header.put("content-type","application/json;charset=UTF-8");
        header.put("origin","https://melody-order.faas.ele.me");
        header.put("referer", "https://melody-order.faas.ele.me/app/query?shopId="+query.get("shopId").toString());
        header.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36");
        header.put("x-eleme-requestid",query.get("requestId").toString());
        header.put("x-shard","shopid="+query.get("shopId").toString());
//        header.put("Cookie", "cna=401WGJwtFDwCAdzDRxlCJn0n; atpsidas=4e5a513a4f3c6a0e38fe78dd_1615188442_10; sca=392fab6a; yunpk=1462711307025378; atpsida=b4996739f8ccf56f428f273c_1616486884_20");
        return HttpUtil.doPost(queryOrderListUrl, jsonObject, header);
    }


    /**
     * 点击出餐 TODO 配置出餐接口的参数
     * @param orderJsonObject
     */
    public void clickButton(JSONObject orderJsonObject , JSONObject allOrderJsonObject,String ksid){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("method","ShipmentService.mealComplete");
        jsonObject.put("id",allOrderJsonObject.getString("id"));
        HashMap metesmap = new HashMap<String,String>();
        metesmap.put("appName","melody");
        metesmap.put("appVersion","1.1.1");
        metesmap.put("key","1.0.0");
        metesmap.put("ksid",ksid);
        jsonObject.put("metas",metesmap);
        jsonObject.put("method","mealComplete");
        jsonObject.put("ncp","2.0.0");



        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("orderId",orderJsonObject.getString("id"));
        paramMap.put("shopId",orderJsonObject.getString("shopId"));
        jsonObject.put("params",paramMap);
        jsonObject.put("service","ShipmentService");



        final Map<String, String> header = new HashMap<String, String>();
        header.put(":authority","app-api.shop.ele.me");
        header.put(":method","POST");
        header.put(":path","/nevermore/invoke/?method=ShipmentService.mealComplete");
        header.put(":scheme","https");
        header.put("accept", "application/json, text/plain, */*");
        header.put("accept-encoding","gzip, deflate");
        header.put("Accept-Language", "zh-CN,zh;q=0.8");
        header.put("cache-control","no-cache");
        header.put("content-type","application/json;charset=UTF-8");
        header.put("origin","https://melody-order.faas.ele.me");
        header.put("pragma","no-cache");
        header.put("referer", "https://melody-order.faas.ele.me/app/query?shopId=" + orderJsonObject.getString("shopId"));
        header.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36");
        header.put("x-eleme-requestid","560C2803FB6E47E08C059744C207B273|1616722792298");
        header.put("x-shard","shopid=" + orderJsonObject.getString("shopId"));
//        header.put("Cookie", "cna=401WGJwtFDwCAdzDRxlCJn0n; atpsidas=4e5a513a4f3c6a0e38fe78dd_1615188442_10; sca=392fab6a; yunpk=1462711307025378; atpsida=b4996739f8ccf56f428f273c_1616486884_20");
        System.out.println("请求夏灿接口 {} 请求数据" + jsonObject);
        String s = HttpUtil.doPost(clickButUrl, jsonObject, header);
        System.out.println("下单返回数据{}：" + s );
    }


}











